package com.studyadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.studyadmin.dto.StuApply;
import com.studyadmin.entity.Classes;
import com.studyadmin.entity.Student;
import com.studyadmin.entity.Teacher;
import com.studyadmin.mapper.ClassesMapper;
import com.studyadmin.mapper.TeacherMapper;
import com.studyadmin.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studyadmin.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 教师表 服务实现类
 * </p>
 *
 * @author lyl
 * @since 2023-03-15
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired(required = false)
    private TeacherMapper teacherMapper;
    @Autowired(required = false)
    private ClassesMapper classesMapper;

    /**
     * 查看申请列表 del_flag = 2
     * @param teaNum 登录人id
     * @return 结果集，申请列表
     */
    @Override
    public JsonUtil stuApplySer(String teaNum) {

//        QueryWrapper<Classes> wrapper = new QueryWrapper<>();
//        wrapper.eq("c.tea_num", teaNum).eq("c.del_flag",2);
        List<StuApply> list=classesMapper.selectApply(teaNum);

        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setCode(0);
        jsonUtil.setData(list);


        return jsonUtil;
    }

    @Override
    public JsonUtil agreeStuApplySer(String stuNum) {
        JsonUtil jsonUtil = new JsonUtil();
        UpdateWrapper<Classes> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("stu_num",stuNum).eq("del_flag",2)
                .set("del_flag",0);
        int res= classesMapper.update(null, updateWrapper);
        if(res>0){
            jsonUtil.setCode(0);
            jsonUtil.setMsg("同意");
        }else {
            jsonUtil.setCode(1);
            jsonUtil.setMsg("未知错误");
        }
        return jsonUtil;
    }

    /**
     * 不同意
     * @param stuNum
     * @return
     */
    @Override
    public JsonUtil disagreeStuApplySer(String stuNum) {
        JsonUtil jsonUtil = new JsonUtil();
        Map<String,Object> map=new HashMap<>();
        map.put("stu_num",stuNum);
        //执行删除
        int res = classesMapper.deleteByMap(map);
//        int res= classesMapper.delete(null, updateWrapper);
        if(res>0){
            jsonUtil.setCode(0);
            jsonUtil.setMsg("不同意");
        }else {
            jsonUtil.setCode(1);
            jsonUtil.setMsg("未知错误");
        }
        return jsonUtil;
    }

    /**
     * 修改教师头像
     * @param teacher
     * @return
     */
    @Override
    public JsonUtil updateTeaImgUrlSer(Teacher teacher) {
        JsonUtil jsonUtil = new JsonUtil();
        UpdateWrapper<Teacher> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("tea_num", teacher.getTeaNum())
                .set("avatar", teacher.getAvatar());
        int resup=teacherMapper.update(null, updateWrapper);
        if(resup<0){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("修改失败");
        }
        jsonUtil.setCode(0);
        jsonUtil.setMsg("修改成功");
        return jsonUtil;
    }


    /**
     * 教师数据  主页
     * @param teaNum
     * @return
     */
    @Override
    public JsonUtil teaMainSer(String teaNum) {
        JsonUtil jsonUtil = new JsonUtil();
        Teacher teacher = teacherMapper.selectOne(
                new QueryWrapper<Teacher>()
                        .select("tea_num","tea_name","sex","avatar","tea_email","del_flag")
                        .eq("tea_num",teaNum)
                        .eq("del_flag",0)
        );
        jsonUtil.setCode(0);
        jsonUtil.setData(teacher);


        return jsonUtil;

    }

    @Override
    public JsonUtil updateTeadentById(Teacher teacher) {
        return null;
    }

}
